<template>
  <div style="height: calc((100% - 70px) * 0.5); margin-top: 15px" v-show="standardId">
    <el-table v-loading="listLoading" :data="list" size="small" height="100%" style="min-height: 200px" v-if="standardId">
      <el-table-column label="序号" type="index" width="50" />
      <el-table-column label="实测实量检查内容" prop="mrqName" show-overflow-tooltip min-width="120" />
      <el-table-column label="评判标准" prop="mrqStandard" show-overflow-tooltip />
      <el-table-column label="选点规则、测量方法" prop="mrqRule" show-overflow-tooltip min-width="130" />
      <el-table-column label="要求测点" prop="requiredCheck" align="center" width="80" />
      <el-table-column align="center" width="120" fixed="right">
        <template slot="header">
          <el-button size="mini" type="text" icon="el-icon-plus" @click="$refs.EditCheckMrq.open({ standardId })" v-hasPermi="['cq:checkMrq:add']">新增</el-button>
        </template>
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="$refs.EditCheckMrq.open(row)" v-hasPermi="['cq:checkMrq:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" :loading="row.removeLoading" @click="btnRemoveItem(row)" v-hasPermi="['cq:checkMrq:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <EditCheckMrq ref="EditCheckMrq" v-model="showEditCheckMrq" @ok="getList" />
  </div>
</template>
<script>
// 实测实量列表
import { debounce } from '@/utils'
import { listCheckMrq, delCheckMrq } from '@/api/qualityManage/cq'
import EditCheckMrq from './EditCheckMrq'
export default {
  name: 'CheckMrqList',
  components: { EditCheckMrq },
  data() {
    return {
      standardId: '',
      listLoading: false,
      list: [],
      showEditCheckMrq: false
    }
  },
  created() {
    this.btnRemoveItem = debounce(this.removeItem, 1e3, true)
  },
  methods: {
    reset() {
      this.standardId = ''
      this.list = []
    },
    open(item = {}) {
      if (!item.standardId) return this.reset()
      this.standardId = item.standardId
      this.getList()
    },
    async getList() {
      const { standardId } = this
      if (!standardId) return this.reset()
      this.listLoading = true
      const res = await listCheckMrq({ standardId }).catch(() => false)
      this.listLoading = false
      if (res === false) return

      const data = res.data || []
      data.map(i => {
        i.removeLoading = false
      })
      data.sort((a, b) => a.mrqName.localeCompare(b.mrqName))
      this.list = data
    },
    async removeItem(item) {
      if (item.removeLoading) return
      const res1 = await this.$confirm(`确认删除数据项：${item.mrqName}?`, '警告', { type: 'warning' }).catch(() => false)
      if (res1 !== 'confirm') return

      item.removeLoading = true
      const res = await delCheckMrq(item.mrqId).catch(() => false)
      item.removeLoading = false
      if (res === false) return

      this.msgSuccess('删除成功')
      this.getList()
    }
  }
}
</script>
